package com.autonavi.yunda.yunji.service;

import com.autonavi.yunda.yunji.core.enums.DatasourceType;
import com.autonavi.yunda.yunji.core.service.IDataSourceConfigService;
import com.autonavi.yunda.yunji.core.vo.localization.DatasourceExportVO;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author cizhong.wcs
 * @date 2023/8/31 17:10
 */
@Component
public class DataSourceConfigService implements IDataSourceConfigService {
    @Override
    public Optional<Pair<DatasourceType, String>> findDataSourceConfigByName(String dataSourceName) {
        for (DatasourceExportVO config : FileLoaderService.datasourceConfigVOS) {
            if (config.getDbname().equals(dataSourceName)) {
                return Optional.of(Pair.of(config.getType(), config.getConfig()));
            }
        }
        return Optional.empty();
    }

    @Override
    public List<Triple<String, DatasourceType, String>> findByDataSourceTypeList(Collection<DatasourceType> dataSourceTypes) {
        List<DatasourceExportVO> suitable = new ArrayList<>();
        dataSourceTypes.forEach(datasourceType -> FileLoaderService.datasourceConfigVOS.stream()
                .filter(config -> config.getType().equals(datasourceType))
                .forEach(suitable::add));
        return suitable.stream()
                .map(result -> Triple.of(result.getDbname(), result.getType(), result.getConfig()))
                .collect(Collectors.toList());
    }
}
